<template>
<van-skeleton title avatar :row="10" :loading="loading">
  <div class="article">
    <!-- 顶部 -->
    <van-nav-bar left-arrow @click-left="$router.back()" fixed>
      <template #title>
        <div class="nav-author" v-show="showNavAuthor">
          <van-image round width="7vw" height="7vw" :src="articleData.aut_photo" />
          <span class="name">{{ articleData.aut_name }}</span>
          <span class="line">|</span>
          <span class="follow" @click="following()" :class="{un:articleData.is_followed}">{{articleData.is_followed?'取消关注':'关注'}}</span>
        </div>
      </template>
      <template #right>
        <van-icon name="ellipsis" size="5.4vw"></van-icon>
      </template>
    </van-nav-bar>

    <!-- 文章主体 -->
    <div class="article-wrapper" @scroll="handleScroll" ref="article">
      <!-- 头部：标题 时间 作者 -->
      <div class="header" ref='myheader'>
        <h3 class="title">
          {{ articleData.title }}
        </h3>
        <div class="time">
          <span>{{ articleData.pubdate }}</span>
          <span>|</span>
          <span>{{ articleData.read_count }} 阅读</span>
          <span>|</span>
          <span>{{ articleData.comm_count }} 评论</span>
        </div>
        <div class="author van-hairline--bottom">
          <van-image
            round
            width="10vw"
            height="10vw"
            src="https://img01.yzcdn.cn/vant/cat.jpeg"
          />
          <span class="name">{{ articleData.aut_name }}</span>
          <van-button v-if="!articleData.is_followed" @click="following()" round size="small" color="#FC6627">+关注</van-button>
          <van-button v-else @click="following()" round size="small">取消关注</van-button>
        </div>
      </div>
      <!-- 内容：文章内容 -->
      <div class="main"  ref="atheader">
        <div class="html" v-html="articleData.content" v-highlight>

        </div>
        <div class="space"></div>
      </div>
      <!-- 评论：评论组件 -->
      <Article-comment :article="articleData" @changeComment="changeComment"></Article-comment>
    </div>
  </div>
  </van-skeleton>
</template>
<script>
import ArticleComment from './components/article-comment.vue'
import { getArticle } from '@/api/article'
import { followingsAuthor } from '@/api/user'
export default {
  components: {
    ArticleComment
  },
  data () {
    return {
      isToComment: false,
      showNavAuthor: false,
      articleData: {},
      loading: true
    }
  },
  methods: {
    changeComment () {
      // 1.取反
      this.isToComment = !this.isToComment
      if (this.isToComment) {
        // 去评论区
        this.$refs.article.scrollTop = this.$refs.atheader.offsetHeight + this.$refs.myheader.offsetHeight
      } else {
        this.$refs.article.scrollTop = 0
      }
    },
    async following () {
      // 1.取消关注或者关注
      const newStatus = !this.articleData.is_followed
      const [err, data] = await followingsAuthor(this.articleData.aut_id, newStatus)

      if (err) return this.$toast.fail(err)
      console.log(data)
      //   if (data.message === 'OK') {
      this.articleData.is_followed = newStatus

      // 如果是关注的了状态  提示关注成功
      this.$toast.success(this.articleData.is_followed ? '关注成功' : '取消关注')
    //   }
    },
    handleScroll () {
    //   console.log(this.$refs.article.scrollTop)
    //   console.log(this.$refs.atheader.offsetTop)
      this.showNavAuthor = this.$refs.article.scrollTop > this.$refs.atheader.offsetTop
    }
  },
  async created () {
    const [err, data] = await getArticle(this.$route.query.id)
    // console.log(this.$route.query.id)
    if (err) return this.$toast.fail(err)
    console.log(data)
    // 赋值数据
    this.articleData = data.data
    // 让loading为false
    this.loading = false
  }
}
</script>
<style scoped lang="less">
.article-wrapper {
  height: 100%;
  overflow-y: auto;
  padding: 44px 0 50px;
  // 头部
  .header {
    padding: 0 16px;
    .title {
      font-size: 20px;
      font-weight: normal;
      padding: 10px 0;
      margin: 0;
    }
    .time {
      font-size: 12px;
      color: #999;
      span:nth-child(2n) {
        margin: 0 5px;
        color: #ccc;
        position: relative;
        top: -1px;
      }
    }
    .author {
      display: flex;
      align-items: center;
      padding: 10px 0;
      .name {
        flex: 1;
        padding-left: 10px;
        font-size: 16px;
      }
    }
  }
  // 内容
  .main {
    .space {
      height: 16px;
      background: @geek-gray-color;
    }
    .html {
      word-break: break-all;
      width: 100%;
      overflow: hidden;
      padding: 20px 16px;
      /deep/ img {
        max-width: 100%;
        background: #f9f9f9;
      }
      /deep/ pre {
        white-space: pre-wrap;
        code {
          white-space: pre;
        }
      }
    }
  }
}

/deep/ .van-nav-bar__title {
   max-width: 270px;
   width: 270px;
}
.nav-author {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  > span {
    font-size: 14px;
    padding-left: 5px;
  }
  .line {
    color: #ccc;
    position: relative;
    top: -1px;
  }
  .follow {
    color: @geek-color;
    &.un {
      color: #999;
    }
  }
}
.article {
    height: 100%;
}
</style>
